package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.MessageUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.CheckLineView;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/5 16:33
 */
public class DelegateTaskChangeFragment extends BaseFragment {
  protected static final String EXTRA_ID =
      DelegateTaskChangeActivity.class.getSimpleName() + "EXTRA_ID";
  @BindView(R.id.tv_newTaskMan) TextView tvNewTaskMan;
  @BindView(R.id.et_taskContentDesc) EditText etTaskContentDesc;
  @BindView(R.id.clv_instationNotify) CheckLineView clvInstationNotify;
  @BindView(R.id.clv_SMSNotify) CheckLineView clvSMSNotify;
  private String taskId;
  private int REQUEST_MAN = 0;
  private String taskManName;
  private String taskManId;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("转交考核人");
    taskId = getArguments().getString(EXTRA_ID);
    taskManId = "";
    taskManName = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        if (TextUtils.isEmpty(tvNewTaskMan.getText().toString()) || TextUtils.isEmpty(
            etTaskContentDesc.getText().toString())) {
          ToastUtil.show(R.string.complete_hint);
        } else {
          volleyPostSubmit();
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_delegate_task_score, container, false);
    ButterKnife.bind(this, view);
    return view;
  }

  private void volleyPostSubmit() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_DELEGATETTASK_CHANGE_MAN);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", taskId);
    params.put("content", etTaskContentDesc.getText().toString());//转交考核人说明
    params.put("newTaskMan", taskManName);//新委派人名称
    params.put("newTaskManId", taskManId);//新委派人id
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        // 解析响应的字符串
        SuccessMessage workProcess = new Gson().fromJson(arg0, SuccessMessage.class);
        if (null == workProcess || null == workProcess.getResult()) {
          ToastUtil.show(R.string.failure_operation);
          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          ToastUtil.show(workProcess.getMessage());
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          if (clvInstationNotify.getCbCheck().isChecked()) {
            String content = getString(R.string.newWorkMessage, getString(R.string.appoint));
            MessageUtil.sendReport(mActivity, taskManId, taskManName, content);
          }
          if (clvSMSNotify.getCbCheck().isChecked()) {
            String content = getString(R.string.newWorkMessage, getString(R.string.appoint));
            MessageUtil.sendSMS(mActivity, taskManId, content);
          }

          ToastUtil.show(R.string.success_operation);
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }
    });
  }

  @OnClick(R.id.tv_newTaskMan) public void onClick() {
    Intent i = new Intent(getActivity(), SearchPersonActivity.class);
    i.putExtra(SearchPersonListFragment.EXTRA_FROM, SearchPersonListFragment.VALUE_COMMON);
    startActivityForResult(i, REQUEST_MAN);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_MAN) {
      CustomerDialog man =
          (CustomerDialog) data.getSerializableExtra(SearchPersonListFragment.EXTRA_VALUE);
      taskManName = man.getName();
      taskManId = man.getJobNo();
      tvNewTaskMan.setText(taskManName);
    }
  }

  public static DelegateTaskChangeFragment newInstance(String taskId) {
    Bundle args = new Bundle();
    args.putString(EXTRA_ID, taskId);
    DelegateTaskChangeFragment fragment = new DelegateTaskChangeFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
